package com.qwert.shortlink.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qwert.shortlink.project.common.convention.result.Result;
import com.qwert.shortlink.project.dto.req.shortlink.ShortLinkCreateReqDTO;
import com.qwert.shortlink.project.dto.req.shortlink.ShortLinkPageReqDTO;
import com.qwert.shortlink.project.dto.req.shortlink.ShortLinkUpdateGroupReqDTO;
import com.qwert.shortlink.project.dto.req.shortlink.ShortLinkUpdateReqDTO;
import com.qwert.shortlink.project.dto.resp.shortlink.ShortLinkCountRespDTO;
import com.qwert.shortlink.project.dto.resp.shortlink.ShortLinkCreateRespDTO;
import com.qwert.shortlink.project.dto.resp.shortlink.ShortLinkPageRespDTO;
import com.qwert.shortlink.project.service.IShortLinkService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2024/7/2 11:22
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/shortlink")
public class ShortLinkController {

    private final IShortLinkService shortLinkService;

//    @GetMapping("/v1/{short-uri}")
//    public void restoreUrl(@PathVariable("short-uri") String shortUri, ServletRequest request, ServletResponse response) throws IOException {
//        shortLinkService.restoreUrl(shortUri, request, response);
//    }

    /**
     * 新增短链接
     */
    @PostMapping("/v1/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO shortLinkCreateReqDTO){
        return Result.success(shortLinkService.createShortLink(shortLinkCreateReqDTO));
    }

    /**
     * 修改短链接信息
     */
    @PostMapping("/v1/update")
    public Result<Integer> updateShortLink(@RequestBody ShortLinkUpdateReqDTO shortLinkUpdateReqDTO){
        return Result.success(shortLinkService.updateShortLink(shortLinkUpdateReqDTO));
    }

    /**
     * 修改短链接分组
     */
    public Result<Integer> updateShortLinkGroup(@RequestBody ShortLinkUpdateGroupReqDTO shortLinkUpdateGroupReqDTO){
        return null;
    }

    /**
     * 分组查询短链接
     */
    @GetMapping("/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO shortLinkPageReqDTO){
        return Result.success(shortLinkService.pageShortLink(shortLinkPageReqDTO));
    }

    /**
     * 查询分组内短链接数量
     */
    @GetMapping("/v1/count")
    public Result<List<ShortLinkCountRespDTO>> listGroupShortLinkCount(@RequestParam("gids")List<String> gids){
        return Result.success(shortLinkService.listGroupShortLinkCount(gids));
    }
}
